package com.chenl.provider.util;

import com.chenl.provider.DTO.vo.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@ConfigurationProperties(prefix = "sso.jwt")
public class SsoJwtUtils {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Value("${secret:f4e2e52034348f86b67cde581c0f9eb1}")
    private String secret;
    private Integer expire = 100;
    private String header;
    private String intercept;
    private SignatureAlgorithm signaturealgorithm = SignatureAlgorithm.HS512;

    public String generateToken(User user) {
        Date nowDate = new Date();
        Date expireDate = new Date(nowDate.getTime() + (long)(this.expire * 1000));
        String token = Jwts.builder().setHeaderParam("type", "JWT").setSubject(user.getPhone() + "").claim("user", user).setIssuedAt(nowDate).setExpiration(expireDate).signWith(signaturealgorithm, this.secret).compact();
        return token;
    }

    public Claims getClaimByToken(String token) {
        try {
            return (Claims)Jwts.parser().setSigningKey(this.secret).parseClaimsJws(token).getBody();
        } catch (Exception var3) {
            log.debug("validate is token error ", var3);
            return null;
        }
    }

    //------------------

    /**
     * 令牌是否过期
     * @param token
     * @return
     */
    public  boolean isTokenExpired(String token) {
        Date expiredDate = getExpiredDateFromToken(token);
        return expiredDate.before(new Date());
    }


    private  Date getExpiredDateFromToken(String token) {
        Claims claims = getClaimsFromToken(token);
        return claims.getExpiration();
    }

    /**
     * 从token中获取JWT中的负载
     */
    private  Claims getClaimsFromToken(String token) {
        Claims claims = null;
        try {
            claims = Jwts.parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            log.error("JWT格式验证失败:{}", token);
        }
        return claims;
    }

    public boolean isTokenExpired(Date expiration) {
        return expiration.before(new Date());
    }

    public static void main(String[] s) {
    }

    public SsoJwtUtils() {
    }

    public Logger getLogger() {
        return this.logger;
    }

    public String getSecret() {
        return this.secret;
    }

    public Integer getExpire() {
        return this.expire;
    }

    public String getHeader() {
        return this.header;
    }

    public String getIntercept() {
        return this.intercept;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public void setExpire(Integer expire) {
        this.expire = expire;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public void setIntercept(String intercept) {
        this.intercept = intercept;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof SsoJwtUtils)) {
            return false;
        } else {
            SsoJwtUtils other = (SsoJwtUtils)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label71: {
                    Object this$logger = this.getLogger();
                    Object other$logger = other.getLogger();
                    if (this$logger == null) {
                        if (other$logger == null) {
                            break label71;
                        }
                    } else if (this$logger.equals(other$logger)) {
                        break label71;
                    }

                    return false;
                }

                Object this$secret = this.getSecret();
                Object other$secret = other.getSecret();
                if (this$secret == null) {
                    if (other$secret != null) {
                        return false;
                    }
                } else if (!this$secret.equals(other$secret)) {
                    return false;
                }

                label57: {
                    Object this$expire = this.getExpire();
                    Object other$expire = other.getExpire();
                    if (this$expire == null) {
                        if (other$expire == null) {
                            break label57;
                        }
                    } else if (this$expire.equals(other$expire)) {
                        break label57;
                    }

                    return false;
                }

                Object this$header = this.getHeader();
                Object other$header = other.getHeader();
                if (this$header == null) {
                    if (other$header != null) {
                        return false;
                    }
                } else if (!this$header.equals(other$header)) {
                    return false;
                }

                Object this$intercept = this.getIntercept();
                Object other$intercept = other.getIntercept();
                if (this$intercept == null) {
                    if (other$intercept == null) {
                        return true;
                    }
                } else if (this$intercept.equals(other$intercept)) {
                    return true;
                }

                return false;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof SsoJwtUtils;
    }

    public int hashCode() {
        int result = 1;
        Object $logger = this.getLogger();
        result = result * 59 + ($logger == null ? 43 : $logger.hashCode());
        Object $secret = this.getSecret();
        result = result * 59 + ($secret == null ? 43 : $secret.hashCode());
        Object $expire = this.getExpire();
        result = result * 59 + ($expire == null ? 43 : $expire.hashCode());
        Object $header = this.getHeader();
        result = result * 59 + ($header == null ? 43 : $header.hashCode());
        Object $intercept = this.getIntercept();
        result = result * 59 + ($intercept == null ? 43 : $intercept.hashCode());
        return result;
    }

    public String toString() {
        return "SsoJwtUtils(logger=" + this.getLogger() + ", secret=" + this.getSecret() + ", expire=" + this.getExpire() + ", header=" + this.getHeader() + ", intercept=" + this.getIntercept() + ")";
    }
}
